{% extends 'layout/manage.html' %}
{% block css %}
    <style>
        .title-list {
            min-height: 100px;
            border-right: 1px solid #dddddd;
        }

        .content {
            min-height: 500px;
            border-left: 1px solid #dddddd;
            margin-left: -1px;
        }

        .title-list ul {
            padding-left: 15px;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }

        .editor-fullscreen {
            z-index: 10001;
        }
    </style>
{% endblock %}
{% block cotent %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading clearfix" style="margin-top: auto"><i class="fa fa-book" aria-hidden="true"></i>
                wiki知识库
                <span><a style="float: right" type="button" class="btn btn-sm btn-success"
                         href="{% url 'wiki_add' project_id=request.manage.id %}">添 加</a></span>
            </div>
            <div class="panel-body">
                <div class="col-xs-3 title-list">
                    <ul id="catalog">

                    </ul>
                </div>
                <div class="col-xs-9 content">
                    <div>
                        <form method="post">
                            {% for item in form %}
                                {% csrf_token %}
                                <div class="form-group">
                                    <label for="">{{ item.label }}</label>
                                    {% if item.name == "cotent" %}
                                        <div id="editor">
                                            {{ item }}
                                        </div>
                                    {% else %}
                                        {{ item }}
                                    {% endif %}
                                    <span class="error">{{ item.errors.0 }}</span>
                                </div>
                            {% endfor %}
                            <input type="submit" class="btn btn-primary" value="提 交">
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block js %}
    <script>
        $(function () {
            init_cata();
            mdediter();
        })

        function mdediter() {
            var upload_img = '{% url 'wiki_upload' project_id=request.manage.id %}'
            editormd("editor", {
                // 这里的尺寸必须在这里设置，设置样式会被 editormd 自动覆盖
                height: "500px",
                // 指定插件路径
                path: "/static/plugin/editor.md/lib/",
                imageUpload: true,
                imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                imageUploadURL: upload_img,
            });
        }

        function init_cata() {
            var wiki_url = '{% url 'wiki' project_id=request.manage.id %}';
            $.ajax({
                url: '{% url 'wiki_catalog' project_id=request.manage.id %}',
                type: "GET",
                dataType: "JSON",
                success: function (res) {
                    if (res.status) {
                        $.each(res.data, function (index, item) {
                            // item=["id":1,"title":目录1，"parent_id":null]
                            var li = $("<li>").attr("id", "id_" + item.id).append($("<a>").text(item.title).attr("href", wiki_url + "?wiki_id=" + item.id)).append($("<ul>"));
                            if (!item.parent_id) {
                                $("#catalog").append(li);
                            } else {
                                $("#id_" + item.parent_id).children("ul").append(li);
                            }
                        })
                    } else {
                        alert("初始化目录失败");
                    }
                }
            })
        }

    </script>

{% endblock %}